package com.gaojiawei.multiThread14_ConcurrentHashMap;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;

/**
 * concurrent hashmap compute method
 */
public class L02_ConcurrentHashMapComputeMehtod {
    public static void main(String[] args) {
        //chage a value of a key
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
        map.put("key",1);
        Integer key = map.get("key");
        map.put("key",key+1);

        map.compute("key", (computekey, computoldvalue) -> {
            if (computoldvalue == null) {
                computoldvalue = 0;
            } else {
                computoldvalue++;
            }
            return computoldvalue;
        });

        System.out.println(map);
    }
}
